package com.hh.controller;

import com.hh.common.BaseResponse;
import com.hh.common.ErrorCode;
import com.hh.common.ResultUtils;
import com.hh.entity.domain.User;
import com.hh.entity.dto.analyze.BlogCategoryAnalyzeRequest;
import com.hh.entity.dto.analyze.BlogTagAnalyzeRequest;
import com.hh.entity.dto.analyze.BlogUserAnalyzeRequest;
import com.hh.entity.vo.analyze.BlogCategoryAnalyzeResponse;
import com.hh.entity.vo.analyze.BlogTagAnalyzeResponse;
import com.hh.entity.vo.analyze.BlogUserAnalyzeResponse;
import com.hh.exception.ThrowUtils;
import com.hh.service.BlogAnalyzeService;
import com.hh.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author hanhui
 * @version 0.0.1
 * @date 2025/3/13
 **/
@RestController
@RequestMapping("/echarts")
public class BlogAnalyzeController {

    @Resource
    private UserService userService;
    @Resource
    private BlogAnalyzeService blogAnalyzeService;


    @PostMapping("/category")
    public BaseResponse<List<BlogCategoryAnalyzeResponse>> getBlogCategoryAnalyze(@RequestBody BlogCategoryAnalyzeRequest blogCategoryAnalyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(blogCategoryAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<BlogCategoryAnalyzeResponse> resultList = blogAnalyzeService.getBlogCategoryAnalyze(blogCategoryAnalyzeRequest, loginUser);
        return ResultUtils.success(resultList);
    }

    @PostMapping("/tag")
    public BaseResponse<List<BlogTagAnalyzeResponse>> getBlogTagAnalyze(@RequestBody BlogTagAnalyzeRequest blogTagAnalyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(blogTagAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = null;
        if (blogTagAnalyzeRequest.getUserId() != null) {
            loginUser = userService.getLoginUser(request);
        }

        List<BlogTagAnalyzeResponse> resultList = blogAnalyzeService.getBlogTagAnalyze(blogTagAnalyzeRequest, loginUser);
        return ResultUtils.success(resultList);
    }


    @PostMapping("/user")
    public BaseResponse<List<BlogUserAnalyzeResponse>> getBlogUserAnalyze(@RequestBody BlogUserAnalyzeRequest blogUserAnalyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(blogUserAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<BlogUserAnalyzeResponse> resultList = blogAnalyzeService.getBlogUserAnalyze(blogUserAnalyzeRequest, loginUser);
        return ResultUtils.success(resultList);
    }

}
